package com.cheyun.netsalev3.repository

import androidx.annotation.MainThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import com.cheyun.netsalev3.dataSource.DataSourceFactory
import com.cheyun.netsalev3.utils.api.NetworkState

/**
 * 分页基础数据处理类
 */
abstract class BasePageRepository<T> :  DataSourceFactory.callbackListener<T> {
    var myCallback: ((Any) -> Unit)? = null
    lateinit var dataSourceFactory: DataSourceFactory<T>
    val networkState = MutableLiveData<NetworkState>()
    var total = MutableLiveData<Int>()


    @MainThread
    fun initSourceFactory(callback: ((Any) -> Unit)? = null) {
        this.myCallback = callback

        dataSourceFactory = DataSourceFactory(this, networkState)
    }

    @MainThread
    fun loadAndroidData(callback: ((Any) -> Unit)? = null): LiveData<PagedList<T>> {
        this.myCallback = callback

        dataSourceFactory = DataSourceFactory(this, networkState)

//        var builder = LivePagedListBuilder(dataSourceFactory, 20)

        var builder = LivePagedListBuilder(dataSourceFactory,
                PagedList.Config.Builder().setPageSize(10).setInitialLoadSizeHint(20).setPrefetchDistance(10).build())
//                .setBackgroundThreadExecutor(Executors.newFixedThreadPool(5))


        return builder.build()
    }
}